package com.yict.csms.system.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.CommonDao;
import com.yict.csms.system.entity.Holiday;
import com.yict.csms.system.entity.UserGroup;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Repository
public class UserGroupDaoImpl extends CommonDao<UserGroup, Long> {
	
	public UserGroup findById(Long id) {
		return (UserGroup)this.getTemplate().get(UserGroup.class, id);
	}

	public boolean check(String userGroupName, Long companyId, Long id) {
		List<UserGroup> list = new ArrayList<UserGroup>();
		StringBuilder queryString = new StringBuilder("from UserGroup as c where 1=1 and c.userGroupName =:userGroupName and c.company.companyId =:companyId");
		if(id != null) {
			queryString.append(" and c.userGroupId not in (:id)");
		}
		try {
			Query query = this.getSession().createQuery(queryString.toString());
			query.setParameter("userGroupName", userGroupName);
			query.setParameter("companyId", companyId);
			if(id != null) {
				query.setParameter("id", id);
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list != null && list.size() > 0) {
			return true;
		}
		return false;
	}
}
